//
//  MainViewController.swift
//  YYJWeibo
//
//  Created by yujing on 16/5/11.
//  Copyright © 2016年 yujing. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
        
        // 添加控制器完成之后改颜色
        // tabBar.tintColor = UIColor.orangeColor()
        
        setupTabbar()
        
        // print(tabBar.subviews)
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        //print(tabBar.subviews)
        
        // 把加号按钮带到前面来
        tabBar.bringSubviewToFront(composeButton)
        
        // 计算宽度
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count) - 1
        composeButton.frame = CGRectInset(tabBar.bounds, width * 2, 0)
    }
    
    // Mark: -监听方法
    @objc private func clickComposeButton() {
        //        print("11111")
        
    }
    
    // Mark: - 私有控件
    /// 撰写按钮 懒加载的方式 {}()
    private lazy var composeButton:UIButton = UIButton(
        yj_title: nil,
        imageName: "tabbar_compose_icon_add",
        backImageName: "tabbar_compose_button")
    
}



// MARK: 设置Tab bar
// 通过 extension 把相关的代码分组放置
extension MainViewController {
    
    // 添加所有子控制器
    private func addChildViewControllers() {
        
        addChildViewController(HomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageViewController(), title: "消息", imageName: "tabbar_message_center")
        
        // 添加空白控制器  占位
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    
    // 添加控制器的封装
    private func addChildViewController(vc: UIViewController,title: String,imageName: String) {
        
        // 控制器
        let vc : UIViewController = vc
        // 设置文字
        vc.title = title
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)
        // 设置图片
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")//?.imageWithRenderingMode(.AlwaysOriginal)
        
        // 创建导航控制器并设置跟控制器为首页控制器
        let nav = UINavigationController(rootViewController: vc)
        
        // 把导航控制器添加到tabbar控制器中
        addChildViewController(nav)
        
    }
    
    // 设置tabbar
    private func setupTabbar() {
        
        tabBar.shadowImage = UIImage.yj_singleDotImage(UIColor(white: 0.9, alpha: 1))
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        
        tabBar .addSubview(composeButton)
        
        composeButton .addTarget(self, action: "clickComposeButton", forControlEvents: .TouchUpInside)
    }
    
}